﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Ciripescu_WebRole.BL;
using Ciripescu_WebRole.Entities;
using Ciripescu_WebRole.Properties;

namespace Ciripescu_WebRole
{
    /// <summary>
    /// Toti cei urmariti de catre un user.(dat in query string)
    /// </summary>
    public partial class allUrmariti : BasePageAnonAndLoggedIn
    {
        protected void RefreshUrmariti(int page)
        {
            string forUserName = String.Empty;

            if (Request.QueryString["userName"] != null)
                forUserName = Request.QueryString["userName"];

            if (string.IsNullOrEmpty(forUserName))
            {
                errors.InnerHtml = Resources.ErrInvalidUser;
                return;
            }

            UserManager um = new UserManager();
            User user = um.GetItem(forUserName);
            if (user == null)
            {
                errors.InnerHtml = Resources.ErrUserNotExists;
                return;
            }
            
            
            lblNrUrmariti.Text       = user.NrUrmariti.ToString() + " " + Resources.Urmariti;
            UrmarireManager urmManag = new UrmarireManager();
            List<User> usersList   = urmManag.GetFollowedByUser(forUserName, page);

            if(page == 1 && 
               usersList.Count < UrmarireManager.m_nbOfUsersPerPage) //prima pagina no next daca avem prea putini useri
                btnNext.Visible = false;

            if (page > 1 &&
                usersList.Count == 0) //cicle to the first page
                //Response.Redirect("/allUrmariti.aspx?page=1");  //revenim la prima pagina cand sau terminat userii
                Response.Redirect("/allUrmariti/" + forUserName + "/1/");
            else
               //populare
                {
                    rptUrmariti.DataSource = usersList;
                    rptUrmariti.DataBind();
                }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserName"] == null)
                Response.Redirect("/");

            if (Request.QueryString["page"] == null)
                Response.Redirect("/");

            if (Request.QueryString["userName"] == null)
                Response.Redirect("/");
            
            m_forUserName = Request.QueryString["userName"];
            
            if (string.IsNullOrEmpty(m_forUserName))
            {
                errors.InnerHtml = Resources.ErrInvalidUser;
                return;
            }

            int page = 1;
            try
            {
                page = Convert.ToInt32(Request.QueryString["page"]);
            }
            catch (Exception ex)
            {
                Utils.WriteLog(EventLogName.Warning, ex.ToString());
                page = 1;
            }

            if (!IsPostBack)
            {
                RefreshUrmariti(page);
            }
        }

        protected void btnRemove_onClick(object sender, EventArgs e)
        {
            ImageButton b        = (ImageButton)sender;
            string toDelete = b.CommandArgument;

            UrmarireManager urmManag = new UrmarireManager();
            Urmarire urmarire        = urmManag.GetItem((string)Session["UserName"], toDelete);
            
            if (urmarire != null)
            {
                urmManag.Delete(urmarire);
            }

            int page = 1;
            try
            {
                page = Convert.ToInt32(Request.QueryString["page"]);
            }
            catch (Exception ex)
            {
                Utils.WriteLog(EventLogName.Warning, ex.ToString());
                page = 1;
            }

            RefreshUrmariti(page);
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            if (Request.QueryString["page"] == null)
                Response.Redirect("/");
            else
            {
                int page = 1;
                try
                {
                    page = Convert.ToInt32(Request.QueryString["page"]);
                }
                catch (Exception ex)
                {
                    Utils.WriteLog(EventLogName.Warning, ex.ToString());
                    page = 1;
                }

                page++; //increase page number

                if (Request.QueryString["UserName"] != null)
                    m_forUserName = Request.QueryString["UserName"];

                if (string.IsNullOrEmpty(m_forUserName))
                {
                    errors.InnerHtml = Resources.ErrInvalidUser;
                    return;
                }
                //Response.Redirect("/allUrmariti.aspx?page=" + page.ToString());
                Response.Redirect("/allUrmariti/" + m_forUserName +"/" + page.ToString() + "/");
            }
        }

        private string m_forUserName;///userii urmariti pentru acest user

        protected void rptUrmariti_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem)
                return;

            ImageButton btnRemove   = (ImageButton)e.Item.FindControl("btnRemove");
            if (m_forUserName == (string)Session["UserName"])
                btnRemove.Visible = true;
            else
                btnRemove.Visible = false;
        }

    }
}
